#include "log.h"
#include <vector>


namespace evm {

    std::shared_ptr<spdlog::logger> LOG;

    void init_log(std::string file_name, bool to_file, bool to_stdout) {
        if (!LOG) {
            try {
                std::vector<spdlog::sink_ptr> sinks;
                if (to_stdout)
                    sinks.push_back(std::make_shared<spdlog::sinks::stdout_sink_mt>());
                if (to_file)
                    sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
                        file_name, "log", 1024 * 1024 * 10, 20));
                LOG = std::make_shared<spdlog::logger>("evm", begin(sinks), end(sinks));
                LOG->set_pattern("[%Y-%m-%d %H:%M:%S.%f] [%l] [%t] %v");
                spdlog::register_logger(LOG);
            }
            catch (const spdlog::spdlog_ex &ex) {
                std::cout << "Log failed: " << ex.what() << std::endl;
                throw;
            }
        }
    }
}
